<template>
  <a-dropdown>
    <div class="header-avatar" style="cursor: pointer">
      <a-avatar class="avatar" size="small" shape="circle" :src="user.avatar"/>
      <span class="name">{{user.name}}</span>
    </div>
    <a-menu :class="['avatar-menu']" slot="overlay">
      <!-- <a-menu-item>
        <a-icon type="user" />
        <span>{{$t('title.personalCenter')}}</span>
      </a-menu-item>
      <a-menu-item>
        <a-icon type="setting" />
        <span>{{$t('title.setting')}}</span>
      </a-menu-item> -->
      <a-menu-item @click="$emit('showChangePasswordModal')">
        <a-icon type="lock" />
        <span>{{$t('title.updatePassword')}}</span>
      </a-menu-item>
      <a-menu-divider />
      <a-menu-item @click="logout">
        <a-icon style="margin-right: 8px;" type="poweroff" />
        <span>{{$t('title.logout')}}</span>
      </a-menu-item>
    </a-menu>
  </a-dropdown>
</template>

<script>
import {mapGetters} from 'vuex'
import {removeAuthorization} from '@/utils/request'
import {accountService} from '@/services'

export default {
  name: 'HeaderAvatar',
  i18n: require('./i18n'),
  computed: {
    ...mapGetters('account', ['user']),
  },
  methods: {
    logout() {
      accountService.logout().then(this.afterLogout)
    },
    afterLogout(res) {
      const logoutRes = res.data
        if (logoutRes.code === 200) {
          this.$router.push('/login')
          localStorage.removeItem(process.env.VUE_APP_ROUTES_KEY)
          localStorage.removeItem(process.env.VUE_APP_PERMISSIONS_KEY)
          localStorage.removeItem(process.env.VUE_APP_ROLES_KEY)
          removeAuthorization()
      }
    }
  }
}
</script>

<style lang="less">
  .header-avatar{
    display: inline-flex;
    .avatar, .name{
      align-self: center;
    }
    .avatar{
      margin-right: 8px;
    }
    .name{
      font-weight: 500;
    }
  }
  .avatar-menu{
    width: 150px;
  }

</style>
